<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: metatags.php 271 2013-09-23 08:26:28Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 271 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 08:26:28 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
*
*	Original by Gunnar Tillmann
*
* ----------------------------------------------------------------------------------- */

global $metaStopWords, $metaGoWords, $metaMinLength, $metaMaxLength, $metaDesLength;
	$metaStopWords 	=	('{$greeting},inkl,Versandkosten,aber,alle,alles,als,auch,auf,aus,bei,beim,beinahe,bin,bis,ist,dabei,dadurch,daher,dank,darum,danach,das,daß,dass,dein,deine,dem,den,der,des,dessen,dadurch,deshalb,die,dies,diese,dieser,diesen,diesem,dieses,doch,dort,durch,eher,ein,eine,einem,einen,einer,eines,einige,einigen,einiges,eigene,eigenes,eigener,endlich,euer,eure,etwas,fast,findet,für,gab,gibt,geben,hatte,hatten,hattest,hattet,heute,hier,hinter,ich,ihr,ihre,ihn,ihm,im,immer,in,ist,ja,jede,jedem,jeden,jeder,jedes,jener,jenes,jetzt,kann,kannst,kein,können,könnt,machen,man,mein,meine,mehr,mit,mußt,musst,müssen,müßt,nach,nachdem,neben,nein,nicht,nichts,noch,nun,nur,oder,statt,anstatt,seid,sein,seine,seiner,sich,sicher,sie,sind,soll,sollen,sollst,sollt,sonst,soweit,sowie,und,uns,unser,unsere,unserem,unseren,unter,vom,von,vor,wann,warum,was,war,weiter,weitere,wenn,wer,werde,widmen,widmet,viel,viele,vieles,weil,werden,werdet,weshalb,wie,wieder,wieso,wir,wird,wirst,wohl,woher,wohin,wurdezum,zur,über');
	$metaGoWords 	=	('mercari, seo, shop, online'); // Hier rein, was nicht gefiltert werden soll
	$metaMinLength 	=	META_MIN_KEYWORD_LENGTH;		// Mindestlänge eines Keywords
	$metaMaxLength 	=	META_MAX_KEYWORD_LENGTH;		// Maximallänge eines Keywords
	$metaDesLength 	=	META_DESC_LENGHT;				// maximale Länge der "description" (in Buchstaben)
	
function metaNoEntities($Text){
    $translation_table = get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
    $translation_table = array_flip($translation_table);
    $Return= strtr($Text,$translation_table);
    return preg_replace( '/&#(\d+);/me',"chr('\\1')",$Return);
}

function metaHtmlEntities($Text) {
	$translation_table=get_html_translation_table(HTML_ENTITIES,ENT_QUOTES);
	$translation_table[chr(38)] = '&';
	return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&amp;",strtr($Text,$translation_table));
}

function prepareWordArray($Text) {
	$Text = str_replace(array('&nbsp;','\t','\r','\n','\b','{$greeting}'),' ',strip_tags($Text));
	$Text = preg_replace("/(&([aou])[^;]*;)/",'$2e',$Text);
	$Text = preg_replace("/(&(s)[^;]*;)/",'$2$2',$Text);
	$Text = preg_replace("/(&([cizen])[^;]*;)/",'$2',$Text);
	$Text = preg_replace("/(&[^;]*;)/",' ',$Text);
	$Text = preg_replace("/([,.])/",'',$Text);
	$Text = trim(preg_replace("/\s\s+/",' ',$Text));
	return($Text);
}

function makeWordArray($Text) {
	$Text = func_get_args();
	$Words = array();
	foreach($Text as $Word)
		if((!empty($Word))&&(is_string($Word)))
			$Words = array_merge($Words, explode(' ', $Word));
	
	return array_unique($Words);
}

function WordArray($Text) {
	return makeWordArray(prepareWordArray($Text));
}

function cleanKeyWords($KeyWords) {
	global $metaStopWords;
	$KeyWords = WordArray(str_replace('"', '', $KeyWords));
	$StopWords = WordArray($metaStopWords);
	$KeyWords = array_diff($KeyWords, $StopWords);
	$KeyWords = array_filter($KeyWords, filterKeyWordArray);
	return $KeyWords;
}

function filterKeyWordArray($KeyWord) {
	global $metaMinLength, $metaMaxLength;
	$GoWords = WordArray(getGoWords());
	if(!in_array($KeyWord, $GoWords)) {
		$Length = strlen($KeyWord);
		if($Length < $metaMinLength)
			return false;
		elseif($Length > $metaMaxLength)
			return false;
	}
	return true;
}

function getGoWords(){
	global $metaGoWords, $categories_meta, $product;
	
	$GoWords = $metaGoWords.' '.META_KEYWORDS;
	
	if(!empty($categories_meta->fields['categories_meta_keywords']))
		$GoWords .= ' '.$categories_meta->fields['categories_meta_keywords'];

	if(!empty($product->data['products_meta_keywords']))
		$GoWords .= ' '.$product->data['products_meta_keywords'];

	return $GoWords;
}

function metaClean($Text,$Length=false,$Abk=' ...') {
	$Text = strip_tags($Text);
	$Text = str_replace(array('&nbsp;','\t','\r','\n','\b','"','{$greeting}'),' ',$Text);
	$Text = trim(preg_replace("/\s\s+/",' ',$Text));
	if(($Length)&&($Length > 0)) {
		if(strlen($Text) > $Length) {
        	$Length -= strlen($Abk);
            $Text = preg_replace('/\s+?(\S+)?$/', '', substr($Text, 0, $Length+1));
            $Text = substr($Text, 0, $Length).$Abk;
		}
	}
	return $Text;
}

function metaTitle($Title=array()) {
	$Title = func_get_args();
	$Title = array_filter($Title, metaClean);
	return implode(' - ',$Title);
}

function metaKeyWords($Text) {
	$KeyWords = cleanKeyWords($Text);
	return implode(', ', $KeyWords);
}

if(isset($_GET['products_id']) && is_numeric($_GET['products_id'])) { // Daten holen: Produkte
	if($product->isProduct()) {
		if(!empty($product->data['products_meta_keywords']))
			$meta_keyw = $product->data['products_meta_keywords'];
		else
			$meta_keyw = metaKeyWords($product->data['products_name'].' '.$product->data['products_description']);

		if(!empty($product->data['products_meta_description'])) {
			$meta_descr = $product->data['products_meta_description'];
			$metaDesLength = false;
		} else
			$meta_descr = $product->data['products_name'].' - '.metaClean($product->data['products_description']);

		if(!empty($product->data['products_meta_title']))
			$meta_title = $product->data['products_meta_title'];
		else
			$meta_title = metaTitle($product->data['products_name'], $product->data['brand_name'], TITLE);
	}
	
} elseif(isset($_GET['brand'])) { // Daten holen: Hersteller
	$brand_meta = $db->db_query("SELECT 
									b.brand_name,
									b.brand_image,
									bi.brand_meta_title,
									bi.brand_meta_description,
									bi.brand_meta_keywords,
									bi.brand_url,
									bi.brand_description 
								FROM 
									".TABLE_BRAND." b,
									".TABLE_BRAND_INFO." bi
								WHERE 
									b.brand_id = '".(int)$_GET['brand']."' 
								AND
									bi.brand_id = '".(int)$_GET['brand']."'
								AND
									bi.languages_id = '".(int)$_SESSION['languages_id']."'", true);
											
	if(!empty($brand_meta->fields['brand_meta_keywords']))
		$meta_keyw = $brand_meta->fields['brand_meta_keywords'];
	else
		$meta_keyw = metaKeyWords($brand_meta->fields['brand_name'].' '.$brand_meta->fields['brand_description']);										
	
	if(!empty($brand_meta->fields['brand_meta_description'])) {
		$meta_descr = $brand_meta->fields['brand_meta_description'];
		$metaDesLength = false;
	} else
		$meta_descr = $brand_meta->fields['brand_name'].' - '.metaClean($brand_meta->fields['brand_description']);
	
	if(!empty($brand_meta->fields['brand_meta_title']))
		$meta_title = $brand_meta->fields['brand_meta_title'];
	else
		$meta_title = metaTitle($brand_meta->fields['brand_name'], TITLE);
	
} elseif(!empty($current_category_id)) { // Daten holen: Kategorie

	$categories_meta = $db->db_query("SELECT
											categories_meta_keywords,
											categories_meta_description,
											categories_meta_title,
											categories_name,
											categories_description
										FROM
											".TABLE_CATEGORIES_DESCRIPTION."
										WHERE
											categories_id = '".$current_category_id."'
										AND
											language_id = '".(int)$_SESSION['languages_id']."'", true);

	$manu_id = $manu_name = false;

	if(isset($_GET['brand']) && !empty($_GET['brand']))
		$manu_id = $_GET['brand'];
	
	if(isset($_GET['filter_id']) && !empty($_GET['filter_id']))
		$manu_id = $_GET['filter_id'];
	
	if($manu_id) {
		$manu_name = $db->db_query("SELECT
										brand_name
									FROM
										".TABLE_BRAND."
									WHERE
										brand_id ='".$manu_id."'", true);
	}
	
	if(!empty($categories_meta->fields['categories_meta_keywords']))
		$meta_keyw = $categories_meta->fields['categories_meta_keywords'];
	else
		$meta_keyw = metaKeyWords($categories_meta->fields['categories_name'].' '.$manu_name->fields['brand_name'].' '.$categories_meta->fields['categories_description']);
	
	if(!empty($categories_meta->fields['categories_meta_description'])) {
		$meta_descr = $categories_meta->fields['categories_meta_description'];
		$metaDesLength = false;
		
	} else{
		if(!empty($categories_meta->fields['categories_name']))
			$meta_descr = TITLE.' - '.metaClean($categories_meta->fields['categories_name']);
		if(!empty($manu_name))
			$meta_descr .= ' von: '.$manu_name->fields['brand_name'];
		if(!empty($categories_meta->fields['categories_description']))
			$meta_descr .= ' - '.metaClean($categories_meta->fields['categories_description']);
	}

	if(!empty($categories_meta->fields['categories_meta_title']))
		$meta_title = metaClean($categories_meta->fields['categories_meta_title']);
	else
		$meta_title = metaTitle($categories_meta->fields['categories_name'], $manu_name->fields['brand_name'], TITLE);
		
} elseif(isset($_GET['coID']) && is_numeric($_GET['coID'])) { //	Daten holen: Inhalts-Seite
	$contents_meta = $db->db_query("SELECT
										content_title,
										content_heading,
										content_text,
										content_meta_title,
										content_meta_description,
										content_meta_keywords
									FROM
										".TABLE_CONTENT_MANAGER."
									WHERE
										content_group='".(int)$_GET['coID']."'
									AND
										languages_id='".(int)$_SESSION['languages_id']."'", true);

	if($contents_meta->RecordCount()) {
		if(!empty($contents_meta->fields['content_meta_title']))
			$meta_title = $contents_meta->fields['content_meta_title'];
		else
			$meta_title	= metaTitle($contents_meta->fields['content_title'],$contents_meta->fields['content_heading'],TITLE);

		if(!empty($contents_meta->fields['content_meta_description']))
			$meta_descr = $contents_meta->fields['content_meta_description'];
		else
			$meta_descr	= $contents_meta->fields['content_heading'].'- '.metaClean($contents_meta->fields['content_text']);

		if(!empty($contents_meta->fields['content_meta_keywords']))
			$meta_keyw = $contents_meta->fields['content_meta_keywords'];
		else
			$meta_keyw	= metaKeyWords($contents_meta->fields['content_title'].' '.$contents_meta->fields['content_heading'].' '.$contents_meta->fields['content_text']);
	}
	
} elseif((isset($_GET['blog_cat']) && is_numeric($_GET['blog_cat'])) &&(!isset($_GET['blog_item']))) { //	Daten holen: Blog Kategorie
	$blog_meta = $db->db_query("SELECT
									meta_key,
									meta_desc,
									meta_title,
									titel
								FROM
									".TABLE_BLOG_CATEGORIES."
								WHERE
									id = '".(int)$_GET['blog_cat']."'
								AND
									language_id = '".(int)$_SESSION['languages_id']."'", true);

	if(!empty($blog_meta->fields['meta_desc']))
		$meta_descr = $blog_meta->fields['meta_desc'];
	else
		$meta_descr = metaTitle($blog_meta->fields['titel'],TITLE);

	if(!empty($blog_meta->fields['meta_key']))
		$meta_keyw = $blog_meta->fields['meta_key'];
	else
		$meta_keyw = metaTitle($blog_meta->fields['titel'],TITLE);

	if(!empty($blog_meta->fields['meta_title']))
		$meta_title = $blog_meta->fields['meta_title'];
	else
		$meta_title = metaTitle($blog_meta->fields['titel'],TITLE);
		
} elseif(isset($_GET['blog_item']) && is_numeric($_GET['blog_item'])){ // Daten holen: Blog Eintrag
	$blog_meta = $db->db_query("SELECT
									meta_keywords,
									meta_description,
									meta_title,
									title
								FROM
									".TABLE_BLOG_ITEMS."
								WHERE
									item_id = '".(int)$_GET['blog_item']."'
								AND
									language_id = '".(int)$_SESSION['languages_id']."'", true);

	if(!empty($blog_meta->fields['meta_description']))
		$meta_descr = metaClean($blog_meta->fields['meta_description'], 300);
	else
		$meta_descr = $blog_meta->fields['name'].' - '.metaClean($blog_meta->fields['description'], 300);

	if(!empty($blog_meta->fields['meta_keywords']))
		$meta_keyw = $blog_meta->fields['meta_keywords'];
	else
		$meta_keyw = metaTitle($blog_meta->fields['titel'],TITLE);

	if(!empty($blog_meta->fields['meta_title']))
		$meta_title = $blog_meta->fields['meta_title'];
	else
		$meta_title = metaTitle($blog_meta->fields['titel'],TITLE);
}

elseif($PHP_SELF == 'specials.php')
	$meta_title = metaTitle(NAVBAR_TITLE_SPECIALS, TITLE);

elseif($PHP_SELF == 'products_new.php')
	$meta_title = metaTitle(NAVBAR_TITLE_PRODUCTS_NEW, TITLE);

if(empty($meta_keyw))
	$meta_keyw = META_KEYWORDS;

if(empty($meta_descr)) {
	$meta_descr = META_DESCRIPTION;
	$metaDesLength = false;
}
if(empty($meta_title))
	$meta_title = TITLE;
?>
<title><?php echo $meta_title;?></title>
<meta name="keywords" content="<?php echo $meta_keyw; ?>" />
<meta name="description" content="<?php echo $meta_descr; ?>" />
<?php
if(GOOGLE_VERIFY !='')
echo '<meta name="google-site-verification" content="'.GOOGLE_VERIFY.'" />'."\n";
?>
<meta name="robots" content="<?php echo META_ROBOTS; ?>" />
<meta name="generator" content="Copyright(c) <?php echo date('Y').' by '.PROJECT_VERSION.PROJECT_VERSION_TYPE ?>" />
<!-- base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER).DIR_WS_CATALOG ?>" / -->
<?php
if(isset($_GET['cPath']) && !$product->isProduct())
	$link = href_link(FILENAME_DEFAULT, 'cPath='.(int)$_GET['cPath']);
elseif($_GET['coID'] == 5)
	$link = HTTP_SERVER.DIR_WS_CATALOG;
else {
	$url = explode('?', $_SERVER['REQUEST_URI']);
	$link = HTTP_SERVER.$url[0];
}
?>
<link href="<?php echo $link ; ?>" rel="canonical" />
<link href="<?php echo DIR_WS_CATALOG; ?>images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="<?php echo DIR_WS_CATALOG; ?>images/favicon.png" rel="shortcut icon" type="image/png" />
